package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.pojo.SysLog;
import com.itheima.dao.SysLogMapper;
import com.itheima.service.SysLogService;
import com.itheima.vo.SysLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    public Page<SysLog> findLogListPage(SysLogVO sysLogVO) {
        if (sysLogVO!=null){
            //设置默认当前页
            if (sysLogVO.getCurrentPage() == null || "".equals(sysLogVO.getCurrentPage())) {
                sysLogVO.setCurrentPage(1);
            }
            //设置默认每页条数
            if (sysLogVO.getPageSize() == null || "".equals(sysLogVO.getPageSize())) {
                sysLogVO.setPageSize(10);
            }
        }else {
            sysLogVO = new SysLogVO();
            sysLogVO.setCurrentPage(1);
            sysLogVO.setPageSize(10);
        }
        Integer currentPage = sysLogVO.getCurrentPage();
        Integer pageSize = sysLogVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(sysLogVO);
        return (Page<SysLog>) sysLogMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param sysLogVO
     * @return
     */

    private Example createExample(SysLogVO sysLogVO){
        Example example=new Example(SysLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (sysLogVO.getUserName()!=null&&!"".equals(sysLogVO.getUserName())){
            criteria.andLike("userName","%"+sysLogVO.getUserName()+"%");
        }
        if (sysLogVO.getOperation()!=null&&!"".equals(sysLogVO.getOperation())){
            criteria.andLike("operation","%"+sysLogVO.getOperation()+"%");
        }
        if (sysLogVO.getStartTime()!=null&&"".equals(sysLogVO.getStartTime())){
            String[] time = sysLogVO.getStartTime().split(",");
            if (time.length==2){
                criteria.andBetween("createTime",time[0],time[1]);
            }
        }
        return example;
    }
}
